package openjdk.reflection;

/**
 * Array Type
 */

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.math.BigDecimal;

public class GetGenericTypeExample2 {
    private int numbers[];
    private BigDecimal[] bigNumbers;
    private BigDecimal[][] bigNumbersOfNumbers;

    public static void main(String... args) throws NoSuchFieldException {
        Field field = GetGenericTypeExample2.class.getDeclaredField("numbers");
        Type type = field.getGenericType();
        printType(field, type);

        Field field2 = GetGenericTypeExample2.class.getDeclaredField("bigNumbers");
        Type type2 = field2.getGenericType();
        printType(field2, type2);

        Field field3 = GetGenericTypeExample2.class.getDeclaredField("bigNumbersOfNumbers");
        Type type3 = field3.getGenericType();
        printType(field3, type3);
    }

    private static void printType(Field field, Type type) {
        System.out.printf("Example: %s %s;%n", field.getType().getSimpleName(), field.getName());
        print(1, "Type class= " + type.getClass());
        print(1, "Type name= " + type.getTypeName());
        printArrayComponentType(type, 1);
    }

    private static void printArrayComponentType(Type type, int level) {
        if (type instanceof Class && ((Class) type).isArray()) {
            Class componentType = ((Class) type).getComponentType();
            print(level, "Array component type=" + componentType.getTypeName());
            printArrayComponentType(componentType, level + 1);
        }
    }

    private static void print(int level, String string) {
        System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string);
    }
}
